const express = require("express");
const bodyParser = require("body-parser");
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use((request, response, next) => {
  console.log("有人请求的服务器2");
  next();
});

app.post("/system/dict/getDictByType", (request, response) => {
  console.log("getDict", request.body);
  const obj = {};
  request.body.forEach((dict) => {
    switch (dict) {
      case "state":
        // 'success' | 'info' | 'warning' | 'danger' | 'primary'
        obj[dict] = [
          { id: 1, name: "正常", code: "1", tagType: "" },
          { id: 2, name: "异常", code: "0", tagType: "danger" },
        ];
        break;
      default:
        const values = [];
        for (let i = 0; i < 3; i++) {
          values.push({ id: i, name: dict + "name" + i, code: dict + i });
        }
        obj[dict] = values;
    }
  });
  response.send(obj);
});
app.get("/cars", (request, response) => {
  const cars = [
    { id: "001", name: "奔驰", price: 199 },
    { id: "002", name: "马自达", price: 109 },
    { id: "003", name: "捷达", price: 120 },
  ];
  response.send(cars);
});

app.listen(5001, (err) => {
  if (!err)
    console.log("服务器启动成功了，请求信息地址：http://localhost:5001");
});
